/**
 * Created by sunga on 2019/1/2.
 */

$().ready(function () {

    $("#add_roleinfo").click(function () {
        // $('#url_info').modal('show');
        $('#role_info').modal({backdrop: 'static', keyboard: false});
        console.log(12121);
        $.ajax({
            url: "/rbac/perm_info/",
            type: "get",
            success: function (data) {
                var data = JSON.parse(data);
                console.log(data, typeof data);

                // 配置中心类型
                 $("#roless label").remove();
                var cc = "";
                $.each(data, function (index, value) {
                    // cc += '<option value="' + value[0] + '"> ' + value[1] + '</option>'
                    console.log(index);
                    console.log(value[0]);
                    var cc = '<div class="form-group" style="margin-left: 20px"><label for="env_name" class="control-label">主界面:</label><div class="card-body"><label class="form-check-label"> <input class="form-check-input" type="checkbox" value="" checked="">' +
                        '<span class="form-check-sign">添加应用信息</span></label><label class="form-check-label"><input class="form-check-input" type="checkbox" value=""><span class="form-check-sign">删除应用信息</span></label></div></div>';
                    cc += ee;
                });
                console.log(cc);
                $("#roless").append(cc);
            }
        });
    });

    $(".edit_userinfo").click(function () {
        // $('#url_info').modal('show');
        $('#edit_user').modal({backdrop: 'static', keyboard: false});
        console.log($(this));
        var proj_id = $(this).attr('data-proj-id');
        // var env_type = $(this).attr('data-env-id');
        console.log(proj_id);
        $.ajax({
            url: "/rbac/userinfo/",
            type: "post",
            data: {"proj_id": proj_id},
            success: function (data) {
                var data = JSON.parse(data);
                console.log(data, typeof data);

                console.log(typeof data["interface_typess"]);
                var interss = data["interface_typess"];
                console.log(interss);
                console.log(typeof interss);

                $("#interface_typesss label").remove();
                var ttinterss = data["interface_type"];
                console.log(ttinterss);
                console.log(typeof ttinterss);

                if (ttinterss == null) {
                    var ee = '&nbsp;&nbsp;&nbsp;&nbsp;<label class="form-check-label"><input class="form-check-input" type="checkbox" name="interface_type" id="checkbox-http" value="http"><span class="form-check-sign">http</span></label>&nbsp;&nbsp;&nbsp;&nbsp;';
                    var ff = '<label class="form-check-label"><input class="form-check-input" type="checkbox" name="interface_type" id="checkbox-dubbo" value="dubbo"><span class="form-check-sign">dubbo</span></label>';

                } else {
                    // var dd = '<label for="configcenter_addr" class="control-label">接口类型:</label><label class="checkbox-inline">';
                    ttinterss = JSON.parse(data["interface_type"]);
                    console.log(ttinterss);
                    console.log(6666666);
                    if (ttinterss.indexOf("http") != -1) {
                        var ee = '&nbsp;&nbsp;&nbsp;&nbsp;<label class="form-check-label"><input class="form-check-input" type="checkbox" name="interface_type" id="checkbox-http" value="http" ><span class="form-check-sign">http</span></label>&nbsp;&nbsp;&nbsp;&nbsp;';
                    } else {
                        var ff = '<label class="form-check-label"><input class="form-check-input" type="checkbox" name="interface_type" id="checkbox-dubbo" value="dubbo" checked="checked"><span class="form-check-sign">dubbo</span></label>';
                    }
                    if (ttinterss.indexOf("dubbo") != -1) {
                        var ff = '<label class="form-check-label"><input class="form-check-input" type="checkbox" name="interface_type" id="checkbox-dubbo" value="dubbo"><span class="form-check-sign">dubbo</span></label>';
                    } else {
                        var ee = '&nbsp;&nbsp;&nbsp;&nbsp;<label class="form-check-label"><input class="form-check-input" type="checkbox" name="interface_type" id="checkbox-http" value="http" checked="checked"><span class="form-check-sign">http</span></label>&nbsp;&nbsp;&nbsp;&nbsp;';
                    }
                }
                console.log(ee);
                console.log(ff);
                gg = ee + ff;
                $("#interface_typesss").append(gg);
            }
        })
    });

    $("#save_url").click(function () {
        if ($("#proj_idddddd").val() == '') {
            console.log("bbbbcuuu");
            var proname = $("#proname").val();
            var app_type = $("#app_type").val();
            var gitaddr = $("#gitaddr").val();
            // var env_type = $("#env_typess").val();
            var optimization = $("#optimizationss").val();
            var optimization_detail = $("#option_detail").val();
            var system_version = $("#system_version").val();
            var java_version = $("#java_version").val();
            var cpu_capacity = $("#cpu_capacity").val();
            var mem_capacity = $("#mem_capacity").val();
            var configcenter = $("#configcenterss").val();
            var configcenter_addr = $("#configcenter_addr").val();

            var proj_id = $(this).attr('data-proj-id');
            var interface_type = new Array();
            $('[name="interface_type"]').each(function (i, e) {
                console.log(i);
                console.log(e);
                console.log($(this).is(':checked'));
                if ($(this).is(':checked') == true) {
                    console.log(e);
                    interface_type.push(e.value);
                }
            });
            console.log(gitaddr);
            console.log(env_type);
            console.log(gitaddr);
            console.log(optimization);
            console.log(optimization_detail);
            console.log(java_version);
            console.log(configcenter);
            console.log(configcenter_addr);
            console.log(interface_type);
            if (app_type == "" ||
                proname == "" ||
                gitaddr == "" ||
                optimization == "" ||
                optimization_detail == "" ||
                system_version == "" ||
                java_version == "" ||
                configcenter == "" ||
                configcenter_addr == ""
            ) {
                swal({
                    title: "",
                    text: "输入不能为空"
                })
            } else {
                $.ajax({
                    url: "/serviceinfo/add/",
                    type: "post",
                    data: {
                        "proname": proname,
                        "app_type": app_type,
                        "gitaddr": gitaddr,
                        "optimization": optimization,
                        "optimization_detail": optimization_detail,
                        "system_version":system_version,
                        "java_version": java_version,
                        "cpu_capacity":cpu_capacity,
                        "mem_capacity":mem_capacity,
                        "configcenter": configcenter,
                        "configcenter_addr": configcenter_addr,
                        "interface_type": JSON.stringify(interface_type)
                    },
                    success: function (data) {
                        var data = JSON.parse(data);
                        console.log(data);
                        if (data["status"] == 200) {
                            window.location.href = '/serviceinfo/';
                            // swal({
                            //     title: "",
                            //     text: "项目创建成功"
                            // }, function () {
                            //     // window.location.href = '/service/connservice_list/?env_type='+ +'&search_app='+ ;
                            //     window.location.href = '/serviceinfo/';
                            // })
                        } else if (data["status"] == 500) {
                            // swal({
                            //     title: "",
                            //     text: "项目已存在"
                            // }
                            console.log("eeeeeeee");

                        }
                    }
                })
            }

        } else {
            var proj_id = $("#proj_idddddd").val();
            var proname = $("#proname").val();
            var gitaddr = $("#gitaddr").val();
            var env_type = $("#env_typess").val();
            var optimization = $("#optimizationss").val();
            var optimization_detail = $("#option_detail").val();
            var java_version = $("#java_version").val();
            var configcenter = $("#configcenterss").val();
            var configcenter_addr = $("#configcenter_addr").val();
            var search_app = $('#save_url').attr("search-app");
            var interface_type = new Array();
            $('[name="interface_type"]').each(function (i, e) {
                console.log(i);
                console.log(e);
                console.log($(this).is(':checked'));
                if ($(this).is(':checked') == true) {
                    console.log(e);
                    interface_type.push(e.value);
                }
            });

            console.log(proj_id);
            console.log(proname);
            console.log(gitaddr);
            console.log(env_type);
            console.log(optimization);
            console.log(optimization_detail);
            console.log(java_version);
            console.log(configcenter);
            console.log(configcenter_addr);
            console.log(interface_type);
            console.log(search_app);

            $.ajax({
                url: "/serviceinfo/saveapp/",
                type: "post",
                data: {
                    "proj_id": proj_id,
                    "proname": proname,
                    "gitaddr": gitaddr,
                    "optimization": optimization,
                    "env_type": env_type,
                    "optimization_detail": optimization_detail,
                    "java_version": java_version,
                    "configcenter": configcenter,
                    "configcenter_addr": configcenter_addr,
                    "search_app": search_app,
                    "interface_type": JSON.stringify(interface_type)
                },
                success: function (data) {
                    var data = JSON.parse(data);
                    console.log(data);
                    if (data["status"] == 200) {
                        swal({
                            title: "",
                            text: "项目更新完成"
                        }, function () {
                            // window.location.href = '/service/connservice_list/';
                            window.location.href = '/service/connservice_list/?env_type='+ data["env_type"] +'&search_app=' + data["search_app"];
                        })
                    } else if (data["status"] == 400) {
                        swal({
                            title: "",
                            text: "项目更新失败"
                        })
                    }
                }
            })
        }

    });


    $(".deleteproj").click(function () {
        var proj_id = $(this).attr('data-proj-id');
        var proname = $(this).attr("data-proj-name");
        console.log("deleteUrl");
        console.log(proj_id);
        var user_id = $(this).attr('data-id');
        swal({
            title: "您确定要删除这条信息吗",
            text: "删除后将无法恢复，请谨慎操作！",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "删除",
            cancelButtonText: "取消",
            closeOnConfirm: false
        }, function () {
            $.ajax({
                url: "/serviceinfo/delete/"+proj_id,
                type: "get",
                success: function (data) {
                    var data = JSON.parse(data);
                    console.log(data);
                    if (data["status"] == 200) {
                        window.location.href = "/serviceinfo/"
                    }

                }
            })
        });
    });
    $(".estiblish").click(function () {
        console.log($(this));
        var proname = $(this).attr("data-proj-name");
        var env_type = $(this).attr("pro-env-type");
        var app_type = $(this).attr("pro-app-type");
        console.log(proname);
        console.log(env_type);
        $.ajax({
            url: "/serviceinfo/jenkins/",
            type: "post",
            data: {"proname": proname,"env_type":env_type,"app_type":app_type},
            success: function (data) {
                var data = JSON.parse(data);
                console.log(data);
                if (data["status"] == 200) {
                    console.log(data);
                    var tt = '<div style="width: 120px" class="progress"><div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width: 100%"> 创建成功</div> </div>';
                    $('[pro-env-type="'+ data["sername"] +'"][pro-env-type="'+ data["env_type"] +'"]').remove();
                    $('[ss_name="'+ data["sername"] +'"][eenv="'+ data["env_type"] +'"]').append(tt);
                }else {
                    var tt = '<div style="width: 120px" class="progress"><div class="progress-bar progress-bar-danger" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width: 100%"> 创建失败</div> </div>';
                    $('[ss_name="'+ data["sername"] +'"][eenv="'+ data["env_type"] +'"]').append(tt);
                }

            }
        })
    })
});